package org.agilelang.generator.javastruts2;

/*
 AgileLang - A fourth generation language(http://www.agilelang.org).

 Copyright (C) 2013 Roderick Cusirramos (roderick20@hotmail.com)

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.util.List;
import org.agilelang.object.Controller;
import org.agilelang.object.attributes.View;


public class GenerateStrutsXML extends Util {

    String path = "";
    String packagefile = "";
    String tiles = "";
    List<Controller> controllers;
   

    public GenerateStrutsXML(String path, List<Controller> listController) {
        this.path = path;
        this.controllers = listController;
        
    }

    public void process() {
        //struts_controller();

        tiles_controller();
        tiles_scaffold();

        /* file = readStreamController("struts.xml.tpl");
        file = file.replace("//pakages// ", packagefile);
        createfile("src/java/struts.xml", path, file);*/

        String file = readStreamController("tiles.xml.tpl");
        file = file.replace("//definition//", tiles);
        createfile("web/WEB-INF/tiles.xml", path, file);
    }

    public void struts_controller() {
        int t = this.controllers.size();
        for (int i = 0; i < t; i++) {
            Controller controller = this.controllers.get(i);
            String views_str = "";
            int tt = controller.views.size();
            for (int ii = 0; ii < tt; ii++) {
                View view = controller.views.get(ii);
                views_str = views_str
                        + "\t\t\t<result name=\"" + view.name.toLowerCase() + "\" type=\"tiles\">" + controller.name.toLowerCase() + ". " + view.name.toLowerCase() + "</result>\r\n";
            }
            packagefile = packagefile
                    + "\t<package name=\"" + controller.name.toLowerCase() + "\" namespace=\"/" + controller.name.toLowerCase() + "\" extends=\"struts-default\">\r\n"
                    + "\t\t<result-types>\r\n"
                    + "\t\t\t<result-type name=\"tiles\" class=\"org.apache.struts2.views.tiles.TilesResult\" />\r\n"
                    + "\t\t</result-types>\r\n"
                    + "\t\t<action name=\"*\" method=\"{1}\" class=\"controller." + controller.name + "Controller\">\r\n"
                    + "\t\t\t<result name=\"index\" type=\"tiles\">" + controller.name.toLowerCase() + ".index</result>\r\n"
                    + views_str
                    + "\t\t</action>\r\n"
                    + "\t</package>\r\n";
        }
    }

    public void tiles_controller() {
        int t = this.controllers.size();
        for (int i = 0; i < t; i++) {
            Controller controller = this.controllers.get(i);
            tiles = tiles
                    + "\t<definition name=\"" + controller.name.toLowerCase() + ".index\" extends=\"baseLayout\">\r\n"
                    + "\t\t<put-attribute name=\"title\"  value=\"" + controller.name + " Index\"/>\r\n"
                    + "\t\t<put-attribute name=\"content\"   value=\"/view/" + controller.name.toLowerCase() + "/index.jsp\"/>\r\n"
                    + "\t</definition>\r\n";

            int tt = controller.views.size();
            for (int ii = 0; ii < tt; ii++) {
                View view = controller.views.get(ii);
                tiles = tiles
                        + "\t<definition name=\"" + controller.name.toLowerCase() + "." + view.name.toLowerCase() + "\" extends=\"baseLayout\">\r\n"
                        + "\t\t<put-attribute name=\"title\"  value=\"" + controller.name + " " + view.name.toLowerCase() + "\"/>\r\n"
                        + "\t\t<put-attribute name=\"content\"   value=\"/view/" + controller.name.toLowerCase() + "/" + view.name.toLowerCase() + ".jsp\"/>\r\n"
                        + "\t</definition>\r\n";
            }
        }


    }

    public void struts_scaffold() {
        /*int t = this.scaffolds.size();
        for (int i = 0; i < t; i++) {
            Scaffold scaffold = this.scaffolds.get(i);
            packagefile = packagefile
                    + "\t<package name=\"" + scaffold.name.toLowerCase() + "\" namespace=\"/" + scaffold.name.toLowerCase() + "\" extends=\"struts-default\">\r\n"
                    + "\t\t<result-types>\r\n"
                    + "\t\t\t<result-type name=\"tiles\" class=\"org.apache.struts2.views.tiles.TilesResult\" />\r\n"
                    + "\t\t</result-types>\r\n"
                    + "\t\t<action name=\"*\" method=\"{1}\" class=\"controller." + scaffold.name + "Controller\">\r\n"
                    + "\t\t\t<result name=\"index\" type=\"tiles\">" + scaffold.name.toLowerCase() + ".index</result>\r\n"
                    + "\t\t\t<result name=\"read\" type=\"tiles\">" + scaffold.name.toLowerCase() + ".read</result>\r\n"
                    + "\t\t\t<result name=\"add\" type=\"tiles\">" + scaffold.name.toLowerCase() + ".add</result>\r\n"
                    + "\t\t\t<result name=\"edit\" type=\"tiles\">" + scaffold.name.toLowerCase() + ".edit</result>\r\n"
                    + "\t\t\t<result name=\"create\" type=\"tiles\">" + scaffold.name.toLowerCase() + ".create</result>\r\n"
                    + "\t\t\t<result name=\"update\" type=\"tiles\">" + scaffold.name.toLowerCase() + ".update</result>\r\n"
                    + "\t\t\t<result name=\"delete\" type=\"tiles\">" + scaffold.name.toLowerCase() + ".delete</result>\r\n"
                    + "\t\t</action>\r\n"
                    + "\t</package>\r\n";
        }*/
    }

    public void tiles_scaffold() {
        
        /*int t = this.scaffolds.size();
        for (int i = 0; i < t; i++) {
            Scaffold scaffold = this.scaffolds.get(i);
            tiles = tiles
                    + "\t<!-- Begin Articles -->\r\n"
                    + "\t<definition name=\"" + scaffold.name.toLowerCase() + ".index\" extends=\"baseLayout\">\r\n"
                    + "\t\t<put-attribute name=\"title\"  value=\"" + scaffold.name + " Index\"/>\r\n"
                    + "\t\t<put-attribute name=\"content\"   value=\"/view/" + scaffold.name.toLowerCase() + "/index.jsp\"/>\r\n"
                    + "\t</definition>\r\n"
                    + "\t<definition name=\"" + scaffold.name.toLowerCase() + ".indexp\" extends=\"baseLayout\">\r\n"
                    + "\t\t<put-attribute name=\"title\"  value=\"" + scaffold.name + " Index\"/>\r\n"
                    + "\t\t<put-attribute name=\"content\"   value=\"/view/" + scaffold.name.toLowerCase() + "/indexp.jsp\"/>\r\n"
                    + "\t</definition>\r\n"
                    + "\t<definition name=\"" + scaffold.name.toLowerCase() + ".read\" extends=\"baseLayout\">\r\n"
                    + "\t\t<put-attribute name=\"title\"  value=\"" + scaffold.name + " Read\"/>\r\n"
                    + "\t\t<put-attribute name=\"content\"   value=\"/view/" + scaffold.name.toLowerCase() + "/read.jsp\"/>\r\n"
                    + "\t</definition>\r\n"
                    + "\t<definition name=\"" + scaffold.name.toLowerCase() + ".add\" extends=\"baseLayout\">\r\n"
                    + "\t\t<put-attribute name=\"title\"  value=\"" + scaffold.name + " Create\"/>\r\n"
                    + "\t\t<put-attribute name=\"content\"   value=\"/view/" + scaffold.name.toLowerCase() + "/create.jsp\"/>\r\n"
                    + "\t</definition>\r\n"
                    + "\t<definition name=\"" + scaffold.name.toLowerCase() + ".edit\" extends=\"baseLayout\">\r\n"
                    + "\t\t<put-attribute name=\"title\"  value=\"" + scaffold.name + " Update\"/>\r\n"
                    + "\t\t<put-attribute name=\"content\"   value=\"/view/" + scaffold.name.toLowerCase() + "/update.jsp\"/>\r\n"
                    + "\t</definition>\r\n"
                    + "\t<!-- End Articles -->\r\n";
        }*/
        
    }
}
